import {Module, MutationTree, ActionTree} from 'vuex';
import {IRootState} from '@/store';
import {login, getUserInfo, getUsers, addUser, updateUser, removeUser} from '@/api/user';
import {setToken, removeToken, getToken} from '@/utils/auth';

// login params
export interface IUserInfo {
  username: string;
  password: string;
}

// 定义state类型
export interface IUserState {
  token: string | null;
  userInfo: Profile | null;
  users: Profile[];
  count: number;
  roles: Role[] | null;
}

// 查询user参数类型
export interface IUserQuery {
  pageNum?: number;
  pageSize?: number;
  mobile?: string;
  status?: boolean;
  username?: string;
}

// 用户编辑/添加查询类型
export type IProfileQuery = Profile & {
  pageNum?: number;
  pageSize?: number;
}

interface Role {
  id: number;
  name: string;
  description: string;
}

export interface Profile {
  avatar: string;
  email: string;
  id: number;
  isSuper: boolean;
  mobile: string;
  status: boolean;
  username: string;
  description: string;
  roles: Role[];
  roleIds?: number[];
  createdAt?: string;
}

// 定义mutation
const mutations: MutationTree<IUserState> = {
  SET_TOKEN(state, token: string) {
    state.token = token;
  },
  SET_USER_INFO(state, data: Profile) {
    state.userInfo = data;
  },
  SET_USERS(state, data: Profile[]) {
    state.users = data;
  },
  SET_COUNT(state, data: number) {
    state.count = data;
  },
  SET_ROLES(state, data: Role[]) {
    state.roles = data;
  }
}

// 定义action
const actions: ActionTree<IUserState, IRootState> = {
  login({commit}, userInfo: IUserInfo) {
    const {username, password} = userInfo
    return new Promise((resolve, reject) => {
      login({username: username.trim(), password}).then(response => {
        const {data} = response
        console.log('data', data)
        commit('SET_TOKEN', data.token)
        setToken(data.token) // localStorage中保存token
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  logout({commit, dispatch}) {
    // todo 请求退出登录接口
    return new Promise<void>((resolve) => {
      // 清空store里token
      commit('SET_TOKEN', '');
      // 清空localStorage里的token
      removeToken();
      // 清除所有tag views 派发的是全局action 需要 root: true
      // https://vuex.vuejs.org/api/#dispatch
      // Dispatch an action. options can have root: true that allows to dispatch root actions in namespaced modules.
      dispatch('tagsView/delAllView', null, {root: true});
      resolve();
    })
  },
  resetToken({commit}) {
    return new Promise<void>((resolve) => {
      // 清空store里token
      commit('SET_TOKEN', '');
      // 清空localStorage里的token
      removeToken();
      resolve();
    })
  },
  getAllUsers({ commit }, params: IUserQuery = {}) {
    return new Promise<void>((resolve, reject) => {
      getUsers(params).then(res => {
        const { data } = res
        commit('SET_USERS', data.users)
        commit('SET_COUNT', data.count)
        resolve()
      }).catch(reject)
    })
  },
  getUserInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getUserInfo().then(response => {
        const {data} = response;
        const {roles, ...info} = data;
        commit('SET_USER_INFO', info);
        commit('SET_ROLES', roles);
        resolve(roles);
      }).catch(reject);
    });
  },
  addUser({ dispatch }, data: IProfileQuery) {
    return new Promise<void>((resolve, reject) => {
      const { pageSize, pageNum, ...params } = data
      addUser(params).then(res => {
        if (res.code === 0) {
          dispatch('getAllUsers', {
            pageSize,
            pageNum
          })
        }
        resolve()
      }).catch(reject)
    })
  },
  editUser({ dispatch }, data: IProfileQuery) {
    return new Promise<void>((resolve, reject) => {
      const { pageSize, pageNum, ...params } = data
      updateUser(params.id, params).then(res => {
        if (res.code === 0) {
          dispatch('getAllUsers', {
            pageSize,
            pageNum
          })
        }
        resolve()
      }).catch(reject)
    })
  },
  removeUser({ dispatch }, data: IProfileQuery) {
    return new Promise<void>((resolve, reject) => {
      const { pageSize, pageNum, id } = data
      removeUser(id).then(res => {
        if (res.code === 0) {
          dispatch('getAllUsers', {
            pageSize,
            pageNum
          })
        }
        resolve()
      }).catch(reject)
    })
  }
}

// 定义user module
const user: Module<IUserState, IRootState> = {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: null,
    users: [],
    count: 0,
    roles: null
  },
  mutations,
  actions
}

export default user
